package top.fzqblog.firstapp;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.fzqblog.customview.MyScrollView;
import top.fzqblog.customview.OnMyScrollChangedListener;
import top.fzqblog.util.StatusBarUtil;

import static top.fzqblog.utils.Constants.TAG;

/**
 * Created by 抽离 on 2017/10/14 0014.
 */

public class MyScrollViewTest extends AppCompatActivity implements OnMyScrollChangedListener {

    @BindView(R.id.container)
    public RelativeLayout container;

    @BindView(R.id.myScrollView)
    public MyScrollView myScrollView;

    @BindView(R.id.ly_container)
    public LinearLayout ly_container;

    @BindView(R.id.ly)
    public LinearLayout ly;

    @BindView(R.id.search01)
    public LinearLayout search01;

    @BindView(R.id.editText)
    public EditText editText;

    @BindView(R.id.iv)
    public ImageView imageView;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_scroll_view);
        ButterKnife.bind(this);
        initTranslucentBar();
        initListener();

    }

    private void initTranslucentBar() {
        StatusBarUtil.setTranslucentForImageView(  this, 0, ly_container);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) ly_container.getLayoutParams();
        layoutParams.setMargins(0, -StatusBarUtil.getStatusBarHeight(this), 0, 0);//上移该容器到状态栏
        ViewGroup.MarginLayoutParams layoutParams2 = (ViewGroup.MarginLayoutParams) ly.getLayoutParams();
        layoutParams2.setMargins(0, StatusBarUtil.getStatusBarHeight(this), 0, 0);//设置图片底下的元素距离上一层有一个状态栏的高度
    }


    private void initListener() {
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                imageHeight = imageView.getHeight();
                myScrollView.setOnMyScrollChangedListener(MyScrollViewTest.this);
            }
        });

    }

    int imageHeight = 0;

    @Override
    public void onScrollChanged(MyScrollView myScrollView, int x, int y, int oldx, int oldy) {
        Log.i(TAG, "onScrollChanged: " + x + "," + y + "," + oldx + "," + oldy);
        Log.i(TAG, "imageview: " + imageHeight);
        if(y >= imageHeight){
            if (editText.getParent()!=search01) {
                ly.removeView(editText);
                search01.addView(editText);
            }
        }
        else{
            if (editText.getParent()!=ly) {
                search01.removeView(editText);
                ly.addView(editText);
            }
        }
    }


}
